﻿namespace WMS6.BLL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.IDAL;
    using WMS6.QueryEntity;
    using WMS6.DALFactory;

    public class SysSettingBLL : IBaseInfo
    {
        private static readonly ICommonMethodDA commonMethod = DataAccess.CreateCommonMethod();
        private List<string> dataBaseID;
        private PubParasInfo pubParasInfo;
        private static readonly ISysSettingDA sysSettingDA = DataAccess.CreateSysSettingDA();

        public SysSettingBLL(string currentWH)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(currentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(currentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001");
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002");
            }
        }

        public SysSettingBLL(PubParasInfo tempPubParasInfo)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(tempPubParasInfo.CurrentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(tempPubParasInfo.CurrentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001", this.pubParasInfo.UserLanguage);
                }
                else
                {
                    this.pubParasInfo = new PubParasInfo();
                    this.pubParasInfo = tempPubParasInfo;
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002", this.pubParasInfo.UserLanguage);
            }
        }

        public string Delete(List<string> IDS)
        {
            throw new NotImplementedException();
        }

        public DataSet GetAllInfo()
        {
            return sysSettingDA.GetAllSysSetting(this.dataBaseID[0]);
        }

        public object GetInfoByID(string id)
        {
            return sysSettingDA.GetSysSettingByID(id, this.dataBaseID[0], this.pubParasInfo);
        }

        public DataSet GetInfoByQueryEntity(object obj)
        {
            SysSettingQueryEntity SysSettingQuery = obj as SysSettingQueryEntity;
            List<string> sqlWhere = new List<string>();
            if (!string.IsNullOrEmpty(SysSettingQuery.SysSettingID))
            {
                sqlWhere.Add(" SS.SYS_SETTING_ID " + SysSettingQuery.SysSettingID);
            }
            if (!string.IsNullOrEmpty(SysSettingQuery.Value))
            {
                sqlWhere.Add(" SS.VALUE " + SysSettingQuery.Value);
            }
            if (!string.IsNullOrEmpty(SysSettingQuery.Value))
            {
                sqlWhere.Add(" SS.VALUE " + SysSettingQuery.Value);
            }
            if (!string.IsNullOrEmpty(SysSettingQuery.ValueDefault))
            {
                sqlWhere.Add(" SS.VALUE_DEFAULT " + SysSettingQuery.ValueDefault);
            }
            if (!string.IsNullOrEmpty(SysSettingQuery.ParamType))
            {
                sqlWhere.Add(" SS.PARAM_TYPE " + SysSettingQuery.ParamType);
            }
            if (!string.IsNullOrEmpty(SysSettingQuery.IsModifiable))
            {
                sqlWhere.Add(" SS.IS_MODIFIABLE " + SysSettingQuery.IsModifiable);
            }
            if (!string.IsNullOrEmpty(SysSettingQuery.Effective))
            {
                sqlWhere.Add(" SS.EFFECTIVE " + SysSettingQuery.Effective);
            }
            return sysSettingDA.GetSysSettingByQueryList(sqlWhere, SysSettingQuery, this.dataBaseID[0], this.pubParasInfo);
        }

        public string Insert(object obj)
        {
            throw new NotImplementedException();
        }

        public string Update(object obj)
        {
            string SysSettingID = string.Empty;
            try
            {
                SysSettingInfo sysSetting = obj as SysSettingInfo;
                sysSetting.UpdatedDate = DateTime.Now;
                SysSettingInfo oldSysSettingInfo = sysSettingDA.GetSysSettingByID(sysSetting.SysSettingID, this.dataBaseID[0], this.pubParasInfo);
                if (sysSettingDA.UpdateSysSetting(sysSetting, this.dataBaseID) > 0)
                {
                    new AuditDataBLL().InsertAuditData<SysSettingInfo>(oldSysSettingInfo, sysSetting, AuditAction.Update, this.pubParasInfo.UserID, "SYS_SETTING", this.dataBaseID[0]);
                    SysSettingID = sysSetting.SysSettingID;
                }
            }
            catch (Exception ex)
            {
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            return SysSettingID;
        }
    }
}

